import Vuex from "vuex";
import config from "../config";
import {
	apiLogin,
	apiFaceCertify,
	apiFaceQuery,
	apiDbArea
} from "@/api/common";
const store = new Vuex.Store({
	state: {
		token: uni.getStorageSync("token") || "",
		userInfo: uni.getStorageSync("token") ? uni.getStorageSync("userInfo") : {},
		cityList: [],
		payFlowRead: uni.getStorageSync("payFlowRead") ? uni.getStorageSync("payFlowRead") : false,
	},
	mutations: {
		SET_TOKEN(state, value) {
			state.token = value;
		},
		SET_USERINFO(state, value) {
			state.userInfo = value;
		},
		SET_CITYLIST(state, value) {
			state.cityList = value;
		},
		SET_PAYFLOWREAD(state, value) {
			state.payFlowRead = value;
			uni.setStorageSync("payFlowRead", value)
		},
	},
	actions: {
		// 登录
		ASYNC_LOGIN({
			commit
		}, cb) {
			// #ifdef MP-ALIPAY
			httplogin(cb);
			// #endif
			// 多端接口请求统一处理
			function httplogin(cb) {
				uni.getAuthCode({
					scopes: "auth_user",
					success: (res) => {
						console.log(res, "3要素");
						uni.showLoading({
							mask: true,
						});
						apiLogin({
							authCode: res.authCode,
						}).then((res) => {
							if (res.code === 200) {
								uni.hideLoading({
									mask: true,
								});
								commit("SET_TOKEN", res.data.token);
								commit("SET_USERINFO", res.data.user);
								uni.setStorageSync({
									key: "token",
									data: res.data.token,
								});
								uni.setStorageSync({
									key: "userInfo",
									data: res.data.user,
								});
								cb && cb(res.data.user);
							}
						});
					},
					fail: (err) => {},
				});
			}
		},
		// 地址库
		ASYNC_CITYLIST({
			commit
		}, value) {
			apiDbArea().then((res) => {
				if (res.code === 200) {
					commit("SET_CITYLIST", res.data.children);
				}
			});
		},
		/**
		 * 唤起认证流程
		 * 参数: certifyId、url 需要通过支付宝 openapi 开放平台网关接口获取
		 * 详细说明可查看文档下方的参数说明
		 **/
		ASYNC_FACE({
			commit,
			state
		}, cb) {
			const {
				token,
				userInfo
			} = state;
			if (!token) {
				return false;
			}
			if (config.env === "dev") {
				cb && cb();
			} else {
				const params = {
					memberName: userInfo.memberName,
					memberPhone: userInfo.memberPhone,
					memberIdNumber: userInfo.memberIdNumber,
				};
				apiFaceCertify(params).then((result) => {
					if (result.code === 200) {
						const {
							alipayUserCertifyOpenCertifyResponse,
							certifyId,
							certifyUrl,
						} = result.data;
						// #ifdef MP-ALIPAY
						my.startAPVerify({
							url: certifyUrl,
							certifyId: certifyId,
							success: function(res) {
								console.log("success", res);
								//只有认证完成后回调，其余情况抛出异常提示
								if (res.resultStatus === "9000") {
									apiFaceQuery({
										certifyId: res.result.certifyId,
									}).then((response) => {
										console.log(response, "response");
										if (response.code === 200) {
											const {
												failReason,
												passed
											} = response.data;
											if (passed === "T") {
												cb && cb();
											} else {
												uni.showToast({
													content: failReason,
													type: "fail",
													mask: true,
												});
											}
										}
									});
								} else if (res.resultStatus === "6002") {
									uni.showToast({
										content: "网络异常",
										type: "fail",
										mask: true,
									});
								} else if (res.resultStatus === "4000") {
									uni.showToast({
										content: "业务异常",
										type: "fail",
										mask: true,
									});
								}
							},
							fail: function(res) {
								console.log("fail", res);
							},
							complete: function(res) {
								console.log("complete", res);
							},
						});
						// #endif
						// #ifdef MP-WEIXIN
						uni.showToast({
							title: "微信人脸识别暂未对接",
							icon: "error",
						});
						// #endif
					}
				});
			}
		},
	},
});
export default store;